
/*    This file is part of the managarm operating system.
 *   Copyright (C) 2007, 2008, 2009  Alexander van der Grinten
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

package org.managarm.jmanacclib.frontend.korona;

import java.util.TreeMap;

//FIXME: make me final
public class KorAstNode {
	// FIXME: remove
	public KorAstNode() { }
	public KorAstNode(int line) {
		setAttribute("Line", new Integer(line));
	}
	// FIXME: remove
	public final int line() {
		if(hasAttribute("Line") == false)
			return -1;
		return getAttrInt("Line");
	}
	
	// stores the attributes of this node
	private final TreeMap<String, Object> attributes
			= new TreeMap<String, Object>();
	
	// adds an attribute to this node
	public final void setAttribute(final String attribute, final Object value) {
		attributes.put(attribute, value);
	}
	
	// returns an attribute
	public final KorAstNode getChild(final String ident) {
		return (KorAstNode)attributes.get(ident);
	}
	public final String getAttrStr(final String attribute) {
		return (String)attributes.get(attribute);
	}
	public final Integer getAttrInt(final String attribute) {
		return (Integer)attributes.get(attribute);
	}
	public final Boolean getAttrBool(final String attribute) {
		return (Boolean)attributes.get(attribute);
	}
	public final Object getAttribute(final String attribute) {
		return attributes.get(attribute);
	}
	public final boolean hasAttribute(final String attribute) {
		return attributes.containsKey(attribute);
	}
	
	// returns true if an attribute exists
	public final Boolean attrBoolIsSet(final String attribute) {
		final Boolean value = getAttrBool(attribute);
		return value == null ? false : value.booleanValue();
	}
	
	// comparison functions for attributes
	public final Boolean attrStrEquals(final String attribute,
			final String operand) {
		final String value = getAttrStr(attribute);
		return value == null ? false : value.equals(operand);
	}
	
	public String toString() {
		String string = new String();
		final java.util.Iterator<String> iterator = attributes.keySet().iterator();
		while(iterator.hasNext()) {
			final String ident = iterator.next();
			string += ident + ": " + attributes.get(ident) + "\n";
		}
		return string;
	}
}
